
#include "ServerConsole.h"

#include <QScrollBar>

#include <QtCore/QDebug>

ServerConsole::ServerConsole(QWidget *parent)
    : QPlainTextEdit(parent)
    , localEchoEnabled(false)
{
    //document()->setMaximumBlockCount(800);
    QPalette p = palette();
    p.setColor(QPalette::Base, Qt::white);
    p.setColor(QPalette::Text, Qt::blue);
    setPalette(p);

}

void ServerConsole::putData(const QByteArray &data)
{
    appendPlainText(QString(data));

//    QScrollBar *bar = verticalScrollBar();
//    bar->setValue(bar->maximum());
}

void ServerConsole::putData(QString &data)
{
    appendPlainText(data);

//    QScrollBar *bar = verticalScrollBar();
//    bar->setValue(bar->maximum());
}

void ServerConsole::setLocalEchoEnabled(bool set)
{
    localEchoEnabled = set;
}

void ServerConsole::keyPressEvent(QKeyEvent *e)
{
    switch (e->key()) {
    case Qt::Key_Backspace:
    case Qt::Key_Left:
    case Qt::Key_Right:
    case Qt::Key_Up:
    case Qt::Key_Down:
        break;
    default:
        if (localEchoEnabled)
            QPlainTextEdit::keyPressEvent(e);
        emit getData(e->text().toLocal8Bit());
    }
}

void ServerConsole::mousePressEvent(QMouseEvent *e)
{
    Q_UNUSED(e)
    setFocus();
}

void ServerConsole::mouseDoubleClickEvent(QMouseEvent *e)
{
    Q_UNUSED(e)
}

void ServerConsole::contextMenuEvent(QContextMenuEvent *e)
{
    Q_UNUSED(e)
}
